package com.union.core.complain.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hnjowe.util.Sms;
import com.opensymphony.xwork2.ActionSupport;
import com.union.core.complain.model.Complain;
import com.union.core.complain.service.ComplainService;
import com.union.core.organization.model.OrganizationManager;
import com.union.core.organization.model.Personnel;
import com.union.core.organization.service.OrganizationManagerService;
import com.union.core.organization.service.PersonnelService;
import com.union.core.sys.service.SysService;

public class ComplainAction extends ActionSupport{
	
	
	private static final long serialVersionUID = 872316012305159L;
	private SysService sysService = SysService.getInstance();
	private static Logger log = Logger.getLogger(ComplainAction.class.getName());
	
	private ComplainService complainService;
	private List<Complain> complainList;
	private Complain complain;
	
	private OrganizationManagerService organizationService;
	private List<OrganizationManager> OrganizationList;
	private OrganizationManager organization;
	
	private PersonnelService personnelService;
	private List<Personnel> personnelList;
	private Personnel personnel;
	
	//投诉信息
	private int complain_id;                   //投诉id                                
	private String complain_name;              //投诉人名称                         
	private String complain_phone;             //投诉人电话
	private int organization_id;
	private String organization_name;          //投诉公会名称                      
	private String complain_content;           //投诉内容                            
	private String add_time;                   //投诉日期                            
	private String handle_user;                // 处理人                               
	private String handle_suggestion;          //处理意见                            
	private String hundle_time;                //处理日期                            
	private int ishandle;                      //是否处理:(1:未处理，2:已处理)
	private String mobile;                     //电话                                  
	private String unit;                       //投诉人单位                         
	private String address;                    //投诉人地址                         
	private String email;                      //投诉人邮箱     
	private String join_date1;
	private String join_date2;
	private int zoneunion;
	private int streetunion;
	private int localunion;
	private int radio;
	private String content;
	
	//分页
	private int start;
	private int intPageSize =15; //每页行数
	private int page = 1;      //页码
	private int intRowCount;   //总行数
	private int intPageCount;  //总页数
	private int intPage;       //页数变量
	private String paramName;
	private String paramValue;
	private String orderByName;
	private List<String> allPower;


	public ComplainAction(){
		complainService = new ComplainService();
		complainList = new ArrayList<Complain>();
		complain = new Complain();
		
		organizationService = new OrganizationManagerService();
		OrganizationList = new ArrayList<OrganizationManager>();
		organization = new OrganizationManager();
		
		personnelService = new PersonnelService();
		personnelList = new ArrayList<Personnel>();
		personnel = new Personnel();
	}

	public String queryList(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			//personnel = personnelService.query(sysService.getSessionListener().getPERSONNEL_ID());
			personnel = sysService.getSessionListener().getPERSONNEL();
			organization_id = personnel.getOrganization_id();
			allPower=sysService.getSessionListener().getAllPower();
				lookup();
				setQuerySession();
				
				return "SUCCESS";
		}	
	}
	
	public String queryGeneralList(){

				lookup();
				setQuerySession();
				
				return "SUCCESS";

	}
	
	/**
	 * 查询列表(多页)
	 * @return String
	 */
	public String pagelist(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			//personnel = personnelService.query(sysService.getSessionListener().getPERSONNEL_ID());
			personnel = sysService.getSessionListener().getPERSONNEL();
			organization_id = personnel.getOrganization_id();
			
			getQuerySession();  //get the query sessions before lookup!
			lookup();
			return "SUCCESS";
		}
	}
	
	/**
	 * 查询列表(多页)
	 * @return String
	 */
	public String pageGenerallist(){
			getQuerySession();  //get the query sessions before lookup!
			lookup();
			return "SUCCESS";

	}
	
	/*
	 * 加载添加机构
	 */
	public String addload(){
		
		OrganizationList = organizationService.queryList("海口市总工会");
		
			return "SUCCESS";
	}
	
	/*
	 * 添加机构
	 */
	public String add(){

			
			complain.setComplain_name(complain_name);
			complain.setComplain_content(complain_content);
			complain.setComplain_phone(complain_phone);
			complain.setOrganization_id(organization_id);
			complain.setOrganization_name(organization_name);
			complain.setIshandle(1);
			complain.setMobile(mobile);
			complain.setAddress(address);
			complain.setUnit(unit);
			complain.setEmail(email);
			
			if(localunion!=0){
				organization = organizationService.query(localunion);
				complain.setOrganization_id(localunion);
				complain.setOrganization_name(organization.getOrganization_name());
			}
			else{
				if(streetunion!=0){
					organization = organizationService.query(streetunion);
					complain.setOrganization_id(streetunion);
					complain.setOrganization_name(organization.getOrganization_name());
				}
				else{
					if(zoneunion!=0){
						organization = organizationService.query(zoneunion);
						complain.setOrganization_id(zoneunion);
						complain.setOrganization_name(organization.getOrganization_name());
					}else{
						complain.setOrganization_name("海口市总工会");
						organization = organizationService.query("海口市总工会");
						complain.setOrganization_id(organization.getOrganization_id());
					}
				}
			}
			
			complainService.add(complain);

			return "SUCCESS";
	}


	/*
	 * 加载修改机构
	 */
	public String editload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			complain = complainService.query(complain_id);

			return "SUCCESS";
		}
	}
	
	/*
	 * 查询机构信息
	 */
	public String query(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			complain = complainService.query(complain_id);
			return "SUCCESS";
		}
	}
	
	public String complainGeneralQuery(){

			complain = complainService.query(complain_id);
			return "SUCCESS";
	}
	/*
	 * 修改机构信息
	 */
	public String edit(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			complain.setHandle_suggestion(handle_suggestion);
			complain.setIshandle(2);
			complain.setHandle_user(sysService.getSessionListener().getMANAGER_NAME());
			
			complainService.edit(complain);

			if(radio==1){
				complain = complainService.query(complain_id);
				/*2.发送短信通知*/
				String sms_text = "尊敬的会员"+complain.getComplain_name()+","+content+".投诉id:"+complain.getComplain_id();
				Sms.getInstance().smsSend(complain.getComplain_phone(), sms_text);
			}
			
			return "SUCCESS";
		}
	}
	
	/*
	 * 删除机构信息
	 */
	public String delete(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			complainService.delete(complain_id);

			return "SUCCESS";
		}
	}
	
	private void lookup(){
		
		if("".equals(complain_id))complain_id = 0;
		if("".equals(complain_name))complain_name = null;
		if("".equals(complain_content))complain_content = null;
		if("".equals(join_date1))join_date1 = null;
		if("".equals(join_date2))join_date2 = null;
		if("".equals(complain_phone))complain_phone = null;
		if("".equals(add_time))add_time = null;
		if("".equals(organization_id))organization_id = 0;
		if("".equals(organization_name))organization_name = null;
		if("".equals(ishandle))ishandle = 0;
		
		if("".equals(paramName))paramName = null;
		if("".equals(paramValue))paramValue = null;
		if("".equals(orderByName))orderByName = null;
		
		if(page<1){
			page = 1;
			intPage = 1;
		}else{
			intPage = page;
		}
		intRowCount = complainService.quertListCount(complain_id, complain_name, complain_content, 
				organization_id,organization_name,join_date1, join_date2, complain_phone, add_time, ishandle,paramName, paramValue);
		intPageCount = (intRowCount + intPageSize -1)/intPageSize;
		if(intPage > intPageCount) intPage = intPageCount;		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
			complainList = complainService.queryList(complain_id, complain_name, complain_content,organization_id,organization_name, 
					join_date1, join_date2, complain_phone, add_time, ishandle,paramName, paramValue, orderByName, start, intPageSize);
	    }
	}
	
	private void setQuerySession(){
		ServletActionContext.getRequest().getSession().setAttribute("complain_id",complain_id); 
		ServletActionContext.getRequest().getSession().setAttribute("complain_name",complain_name); 
		ServletActionContext.getRequest().getSession().setAttribute("complain_content",complain_content); 
		ServletActionContext.getRequest().getSession().setAttribute("organization_id",organization_id); 
		ServletActionContext.getRequest().getSession().setAttribute("organization_name",organization_name); 
		ServletActionContext.getRequest().getSession().setAttribute("join_date1",join_date1); 
		ServletActionContext.getRequest().getSession().setAttribute("join_date2",join_date2); 
		ServletActionContext.getRequest().getSession().setAttribute("complain_phone",complain_phone); 
		ServletActionContext.getRequest().getSession().setAttribute("add_time",add_time); 
		ServletActionContext.getRequest().getSession().setAttribute("paramName",paramName); 
		ServletActionContext.getRequest().getSession().setAttribute("paramValue",paramValue); 
		ServletActionContext.getRequest().getSession().setAttribute("orderByName",orderByName); 
	}
	
	private void getQuerySession(){
		complain_id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("complain_id");
		complain_name = (String)ServletActionContext.getRequest().getSession().getAttribute("complain_name");
		complain_content = (String)ServletActionContext.getRequest().getSession().getAttribute("complain_content");
		organization_id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("organization_id");
		organization_name = (String)ServletActionContext.getRequest().getSession().getAttribute("organization_name");
		join_date1 = (String)ServletActionContext.getRequest().getSession().getAttribute("join_date1");
		join_date2 = (String)ServletActionContext.getRequest().getSession().getAttribute("join_date2");
		complain_phone = (String)ServletActionContext.getRequest().getSession().getAttribute("complain_phone");
		add_time = (String)ServletActionContext.getRequest().getSession().getAttribute("add_time");
		paramName = (String)ServletActionContext.getRequest().getSession().getAttribute("paramName");
		paramValue = (String)ServletActionContext.getRequest().getSession().getAttribute("paramValue");
		orderByName = (String)ServletActionContext.getRequest().getSession().getAttribute("orderByName");
	}
	public SysService getSysService() {
		return sysService;
	}

	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}

	public String getAdd_time() {
		return add_time;
	}

	public void setAdd_time(String addTime) {
		add_time = addTime;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getIntPageSize() {
		return intPageSize;
	}

	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getIntRowCount() {
		return intRowCount;
	}

	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}

	public int getIntPageCount() {
		return intPageCount;
	}

	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}

	public int getIntPage() {
		return intPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	public String getParamName() {
		return paramName;
	}

	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	public String getParamValue() {
		return paramValue;
	}

	public void setParamValue(String paramValue) {
		this.paramValue = paramValue;
	}

	public String getOrderByName() {
		return orderByName;
	}

	public void setOrderByName(String orderByName) {
		this.orderByName = orderByName;
	}

	public String getJoin_date1() {
		return join_date1;
	}

	public void setJoin_date1(String joinDate1) {
		join_date1 = joinDate1;
	}

	public String getJoin_date2() {
		return join_date2;
	}

	public void setJoin_date2(String joinDate2) {
		join_date2 = joinDate2;
	}

	public List<Complain> getComplainList() {
		return complainList;
	}

	public void setComplainList(List<Complain> complainList) {
		this.complainList = complainList;
	}

	public Complain getComplain() {
		return complain;
	}

	public void setComplain(Complain complain) {
		this.complain = complain;
	}

	public int getComplain_id() {
		return complain_id;
	}

	public void setComplain_id(int complainId) {
		complain_id = complainId;
	}

	public String getComplain_name() {
		return complain_name;
	}

	public void setComplain_name(String complainName) {
		complain_name = complainName;
	}

	public String getComplain_phone() {
		return complain_phone;
	}

	public void setComplain_phone(String complainPhone) {
		complain_phone = complainPhone;
	}

	public int getOrganization_id() {
		return organization_id;
	}

	public void setOrganization_id(int organizationId) {
		organization_id = organizationId;
	}

	public String getOrganization_name() {
		return organization_name;
	}

	public void setOrganization_name(String organizationName) {
		organization_name = organizationName;
	}

	public String getComplain_content() {
		return complain_content;
	}

	public void setComplain_content(String complainContent) {
		complain_content = complainContent;
	}

	public String getHandle_user() {
		return handle_user;
	}

	public void setHandle_user(String handleUser) {
		handle_user = handleUser;
	}

	public String getHandle_suggestion() {
		return handle_suggestion;
	}

	public void setHandle_suggestion(String handleSuggestion) {
		handle_suggestion = handleSuggestion;
	}

	public String getHundle_time() {
		return hundle_time;
	}

	public void setHundle_time(String hundleTime) {
		hundle_time = hundleTime;
	}

	public int getIshandle() {
		return ishandle;
	}

	public void setIshandle(int ishandle) {
		this.ishandle = ishandle;
	}

	public List<OrganizationManager> getOrganizationList() {
		return OrganizationList;
	}

	public void setOrganizationList(List<OrganizationManager> organizationList) {
		OrganizationList = organizationList;
	}

	public OrganizationManager getOrganization() {
		return organization;
	}

	public void setOrganization(OrganizationManager organization) {
		this.organization = organization;
	}

	public int getZoneunion() {
		return zoneunion;
	}

	public void setZoneunion(int zoneunion) {
		this.zoneunion = zoneunion;
	}

	public int getStreetunion() {
		return streetunion;
	}

	public void setStreetunion(int streetunion) {
		this.streetunion = streetunion;
	}

	public int getLocalunion() {
		return localunion;
	}

	public void setLocalunion(int localunion) {
		this.localunion = localunion;
	}

	public List<Personnel> getPersonnelList() {
		return personnelList;
	}

	public void setPersonnelList(List<Personnel> personnelList) {
		this.personnelList = personnelList;
	}

	public Personnel getPersonnel() {
		return personnel;
	}

	public void setPersonnel(Personnel personnel) {
		this.personnel = personnel;
	}
	
	public void setRadio(int radio) {
		this.radio = radio;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	public List<String> getAllPower() {
		return allPower;
	}
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}
}